

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>speechemotionrecognition &mdash; Speech Emotion Recognition 1.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../_static/jquery.js"></script>
        <script type="text/javascript" src="../_static/underscore.js"></script>
        <script type="text/javascript" src="../_static/doctools.js"></script>
        <script type="text/javascript" src="../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../main.html" class="icon icon-home"> Speech Emotion Recognition
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../modules.html">speechemotionrecognition</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../main.html">Speech Emotion Recognition</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../main.html">Docs</a> &raquo;</li>
        
          <li><a href="index.html">Module code</a> &raquo;</li>
        
      <li>speechemotionrecognition</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for speechemotionrecognition</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">speechemotionrecognition module.</span>
<span class="sd">Provides a library to perform speech emotion recognition on `emodb` data set</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="k">import</span> <span class="n">Tuple</span>

<span class="kn">import</span> <span class="nn">numpy</span>
<span class="kn">from</span> <span class="nn">sklearn.metrics</span> <span class="k">import</span> <span class="n">accuracy_score</span><span class="p">,</span> <span class="n">confusion_matrix</span>

<span class="n">__author__</span> <span class="o">=</span> <span class="s1">&#39;harry-7&#39;</span>
<span class="n">__version__</span> <span class="o">=</span> <span class="s1">&#39;1.1&#39;</span>


<div class="viewcode-block" id="Model"><a class="viewcode-back" href="../speechemotionrecognition.html#speechemotionrecognition.Model">[docs]</a><span class="k">class</span> <span class="nc">Model</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Model is the abstract class which determines how a model should be.</span>
<span class="sd">    Any model inheriting this class should do the following.</span>

<span class="sd">    1.  Set the model instance variable to the corresponding model class which</span>
<span class="sd">        which will provide methods `fit` and `predict`.</span>

<span class="sd">    2.  Should implement the following abstract methods `load_model`,</span>
<span class="sd">        `save_model` `train` and `evaluate`. These methods provide the</span>
<span class="sd">        functionality to save the model to the disk, load the model from the</span>
<span class="sd">        disk and train the model and evaluate the model to return appropriate</span>
<span class="sd">        measure like accuracy, f1 score, etc.</span>

<span class="sd">    Attributes:</span>
<span class="sd">        model (Any): instance variable that holds the model.</span>
<span class="sd">        save_path (str): path to save the model.</span>
<span class="sd">        name (str): name of the model.</span>
<span class="sd">        trained (bool): True if model has been trained, false otherwise.</span>
<span class="sd">    &quot;&quot;&quot;</span>

<div class="viewcode-block" id="Model.__init__"><a class="viewcode-back" href="../speechemotionrecognition.html#speechemotionrecognition.Model.__init__">[docs]</a>    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">save_path</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">name</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;Not Specified&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Default constructor for abstract class Model.</span>

<span class="sd">        Args:</span>
<span class="sd">            save_path(str): path to save the model to.</span>
<span class="sd">            name(str): name of the model given as string.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Place holder for model</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="c1"># Place holder on where to save the model</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">save_path</span> <span class="o">=</span> <span class="n">save_path</span>
        <span class="c1"># Place holder for name of the model</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
        <span class="c1"># Model has been trained or not</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">trained</span> <span class="o">=</span> <span class="kc">False</span></div>

<div class="viewcode-block" id="Model.train"><a class="viewcode-back" href="../speechemotionrecognition.html#speechemotionrecognition.Model.train">[docs]</a>    <span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x_train</span><span class="p">:</span> <span class="n">numpy</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span> <span class="n">y_train</span><span class="p">:</span> <span class="n">numpy</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span>
              <span class="n">x_val</span><span class="p">:</span> <span class="n">numpy</span><span class="o">.</span><span class="n">ndarray</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
              <span class="n">y_val</span><span class="p">:</span> <span class="n">numpy</span><span class="o">.</span><span class="n">ndarray</span> <span class="o">=</span> <span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Trains the model with the given training data.</span>

<span class="sd">        Args:</span>
<span class="sd">            x_train (numpy.ndarray): samples of training data.</span>
<span class="sd">            y_train (numpy.ndarray): labels for training data.</span>
<span class="sd">            x_val (numpy.ndarray): Optional, samples in the validation data.</span>
<span class="sd">            y_val (numpy.ndarray): Optional, labels of the validation data.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># This will be specific to model so should be implemented by</span>
        <span class="c1"># child classes</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span></div>

<div class="viewcode-block" id="Model.predict"><a class="viewcode-back" href="../speechemotionrecognition.html#speechemotionrecognition.Model.predict">[docs]</a>    <span class="k">def</span> <span class="nf">predict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">samples</span><span class="p">:</span> <span class="n">numpy</span><span class="o">.</span><span class="n">ndarray</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Tuple</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Predict labels for given data.</span>

<span class="sd">        Args:</span>
<span class="sd">            samples (numpy.ndarray): data for which labels need to be predicted</span>

<span class="sd">        Returns:</span>
<span class="sd">            list: list of labels predicted for the data.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">results</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">sample</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">samples</span><span class="p">):</span>
            <span class="n">results</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">predict_one</span><span class="p">(</span><span class="n">sample</span><span class="p">))</span>
        <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">results</span><span class="p">)</span></div>

<div class="viewcode-block" id="Model.predict_one"><a class="viewcode-back" href="../speechemotionrecognition.html#speechemotionrecognition.Model.predict_one">[docs]</a>    <span class="k">def</span> <span class="nf">predict_one</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sample</span><span class="p">:</span> <span class="n">numpy</span><span class="o">.</span><span class="n">ndarray</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Predict label of a single sample. The reason this method exists is</span>
<span class="sd">        because often we might want to predict label for a single sample.</span>

<span class="sd">        Args:</span>
<span class="sd">            sample (numpy.ndarray): Feature vector of the sample that we want to</span>
<span class="sd">                                    predict the label for.</span>

<span class="sd">        Returns:</span>
<span class="sd">            int: returns the label for the sample.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># This need to be implemented for the child models. The reason is that</span>
        <span class="c1"># ML models and DL models predict the labels differently.</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span></div>

<div class="viewcode-block" id="Model.restore_model"><a class="viewcode-back" href="../speechemotionrecognition.html#speechemotionrecognition.Model.restore_model">[docs]</a>    <span class="k">def</span> <span class="nf">restore_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">load_path</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Restore the weights from a saved model and load them to the model.</span>

<span class="sd">        Args:</span>
<span class="sd">            load_path (str): Optional, path to load the weights from a given path.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">to_load</span> <span class="o">=</span> <span class="n">load_path</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">save_path</span>
        <span class="k">if</span> <span class="n">to_load</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span>
                <span class="s2">&quot;Provide a path to load from or save_path of the model</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
            <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">load_model</span><span class="p">(</span><span class="n">to_load</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">trained</span> <span class="o">=</span> <span class="kc">True</span></div>

<div class="viewcode-block" id="Model.load_model"><a class="viewcode-back" href="../speechemotionrecognition.html#speechemotionrecognition.Model.load_model">[docs]</a>    <span class="k">def</span> <span class="nf">load_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">to_load</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Load the weights from the given saved model.</span>

<span class="sd">        Args:</span>
<span class="sd">            to_load: path containing the saved model.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># This will be specific to model so should be implemented by</span>
        <span class="c1"># child classes</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span></div>

<div class="viewcode-block" id="Model.save_model"><a class="viewcode-back" href="../speechemotionrecognition.html#speechemotionrecognition.Model.save_model">[docs]</a>    <span class="k">def</span> <span class="nf">save_model</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Save the model to path denoted by `save_path` instance variable.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># This will be specific to model so should be implemented by</span>
        <span class="c1"># child classes</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span></div>

<div class="viewcode-block" id="Model.evaluate"><a class="viewcode-back" href="../speechemotionrecognition.html#speechemotionrecognition.Model.evaluate">[docs]</a>    <span class="k">def</span> <span class="nf">evaluate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x_test</span><span class="p">:</span> <span class="n">numpy</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span> <span class="n">y_test</span><span class="p">:</span> <span class="n">numpy</span><span class="o">.</span><span class="n">ndarray</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Evaluate the current model on the given test data.</span>

<span class="sd">        Predict the labels for test data using the model and print the relevant</span>
<span class="sd">        metrics like accuracy and the confusion matrix.</span>

<span class="sd">        Args:</span>
<span class="sd">            x_test (numpy.ndarray): Numpy nD array or a list like object</span>
<span class="sd">                                    containing the samples.</span>
<span class="sd">            y_test (numpy.ndarray): Numpy 1D array or list like object</span>
<span class="sd">                                    containing the labels for test samples.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">predictions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">x_test</span><span class="p">)</span>
        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Accuracy:</span><span class="si">%.3f</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">accuracy_score</span><span class="p">(</span><span class="n">y_pred</span><span class="o">=</span><span class="n">predictions</span><span class="p">,</span>
                                                 <span class="n">y_true</span><span class="o">=</span><span class="n">y_test</span><span class="p">))</span>
        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Confusion matrix:&#39;</span><span class="p">,</span> <span class="n">confusion_matrix</span><span class="p">(</span><span class="n">y_pred</span><span class="o">=</span><span class="n">predictions</span><span class="p">,</span>
                                                    <span class="n">y_true</span><span class="o">=</span><span class="n">y_test</span><span class="p">))</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, Hemanth Kumar Veeranki

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>